//
//  MDRecommendViewController.swift
//  麦兜直播
//
//  Created by Apple on 16/10/9.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

private let NORMALID = "MDNormalCellID"
private let BEAUTYID = "MDBeautyCellID"

private let margin:CGFloat = 10.0
private let itemW = (screenWidth - 3*margin)*0.5
private let gamelistH:CGFloat = 80
private let cycleH:CGFloat = screenWidth/3

import UIKit

class MDRecommendViewController: MDBaseRoomController {
   fileprivate lazy var homeNetTool:MDHomeNetTool = MDHomeNetTool()
   fileprivate lazy var gameListView:MDGameListView = MDGameListView.show()
   fileprivate lazy var cycleScrollView:MDCycleScrollView = {
      let cycleView = MDCycleScrollView.show()
       cycleView.frame = CGRect(x: 0, y: -(cycleH+gamelistH), width: screenWidth, height: cycleH)
      return cycleView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        requestScrollData()
        collectionView.register(UINib(nibName: "MDBeautyCell", bundle: nil), forCellWithReuseIdentifier: BEAUTYID)
    }
}


extension MDRecommendViewController{
    override func setUpUI(){
        super.setUpUI()
        gameListView.frame = CGRect(x: 0, y: -gamelistH, width: screenWidth, height: gamelistH)
        collectionView.addSubview(gameListView)
        collectionView.addSubview(cycleScrollView)
        cycleScrollView.backgroundColor = UIColor.red
        collectionView.contentInset = UIEdgeInsets(top: gamelistH+cycleH, left: 0, bottom:0, right: 0)
    }
    
    
  override func requestData() {
        baseNetTool = homeNetTool
        homeNetTool.reuestData {
            self.stopAnimation()
            self.collectionView.reloadData()
            var homeGroup = self.homeNetTool.baseRoomGroup
            homeGroup.removeFirst()
            homeGroup.removeFirst()
            
            let hGroup = MDHomeGroup()
            hGroup.tag_name = "更多"
            homeGroup.append(hGroup)
            self.gameListView.homeGroup = homeGroup
        }
    
    }
    
    //请求轮播数据
    fileprivate func requestScrollData(){
        self.homeNetTool.requestScrollData {
            
            self.cycleScrollView.cycleGroup = self.homeNetTool.cycleDatas
            
        }
    }
}


// MARK:- 数据源和代理方法
extension MDRecommendViewController{
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let homeGroup = homeNetTool.baseRoomGroup[indexPath.section]
        let home = homeGroup.rooms[indexPath.row]

        if (indexPath as NSIndexPath).section == 1{
            let  beautyCell  = collectionView.dequeueReusableCell(withReuseIdentifier: BEAUTYID, for: indexPath) as!MDBeautyCell
            beautyCell.room = home
            return beautyCell
            
        }else{
            let normalCell = collectionView.dequeueReusableCell(withReuseIdentifier: NORMALID, for: indexPath) as!MDNormalCell
            normalCell.home = home
            return normalCell
        }
    }
    

    
    override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if (indexPath as NSIndexPath).section == 1 {
            return CGSize(width: itemW, height: itemW*5/4)
        }
        
        return CGSize(width: itemW, height: itemW*3/4)
    }
}





